import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rental/common/routers/names.dart';
import 'package:rental/pages/index.dart';



class MainPage extends StatelessWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const _MainViewGetX();
  }
}

class _MainViewGetX extends GetView<MainController> {
  const _MainViewGetX({Key? key}) : super(key: key);

  // 根据路由名称获取页面组件
  Widget _getPageByRoute(String routeName) {
    switch (routeName) {
      case RouteNames.rentalDeviceIndex:
        return const DeviceIndexPage() ;
      case RouteNames.orderOrderIndex:
        return const OrderIndexPage() ;
      case RouteNames.msgMsgIndex:
        return const MsgIndexPage() ;
      case RouteNames.myMyIndex:
        return const MyIndexPage();
      default:
        return const DeviceIndexPage(); // 默认页面
    }
  }

  // 构建页面容器 - 使用IndexedStack保持页面状态，通过路由获取页面
  Widget _buildPageContainer() {
    return IndexedStack(
      index: controller.currentIndex,
      children: controller.tabRoutes.map((routeName) => _getPageByRoute(routeName)).toList(),
    );
  }

  // 构建自定义底部导航栏 - 去掉水波纹效果，添加点击放大缩小动画
  Widget _buildBottomNavigationBar() {
    return Container(
      height: 80,
      decoration: const BoxDecoration(
        color: Colors.white,
        border: Border(
          top: BorderSide(color: Colors.grey, width: 0.5),
        ),
      ),
      child: Row(
        children: List.generate(
          controller.tabTitles.length,
          (index) => Expanded(
            child: _buildTabItem(index),
          ),
        ),
      ),
    );
  }

  // 构建单个导航栏项目 - 带点击动画效果
  Widget _buildTabItem(int index) {
    final isSelected = controller.currentIndex == index;
    
    return GetBuilder<MainController>(
      id: "tab_$index",
      builder: (_) {
        return AnimatedScale(
          scale: controller.getTabScale(index),
          duration: const Duration(milliseconds: 150),
          curve: Curves.easeInOut,
          child: GestureDetector(
            onTap: () {
              controller.onTabTap(index);
            },
            onTapDown: (_) {
              controller.onTabTapDown(index);
            },
            onTapUp: (_) {
              controller.onTabTapUp(index);
            },
            onTapCancel: () {
              controller.onTabTapCancel(index);
            },
            child: Container(
              color: Colors.transparent,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    controller.tabIcons[index],
                    style: TextStyle(
                      fontSize: 20,
                      color: isSelected ? Colors.blue : Colors.grey,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    controller.tabTitles[index],
                    style: TextStyle(
                      fontSize: 12,
                      color: isSelected ? Colors.blue : Colors.grey,
                      fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
      id: "main",
      builder: (_) {
        return Scaffold(
          // appBar: AppBar(
          //   title: Text(controller.tabTitles[controller.currentIndex]),
          //   backgroundColor: Colors.blue,
          //   foregroundColor: Colors.white,
          // ),
          body: _buildPageContainer(),
          bottomNavigationBar: _buildBottomNavigationBar(),
        );
      },
    );
  }
}
